#include "stm32f4xx.h"
#include <stdbool.h>
#include "canfestival.h"
#include "timer_canfestival.h"

//#define TIM5_isr TIM5_IRQHandler
/************************** Modul variables **********************************/
// Store the last timer value to calculate the elapsed time
static TIMEVAL last_time_set = TIMEVAL_MAX;
vu16 CCR1_Val = 49152;

void setTimer(TIMEVAL value)
{
	u16 capture = 0;
	//static bool bLEDOn = TRUE;

	capture = TIM_GetCapture1(TIM4);
	TIM_SetCompare1(TIM4, capture + value);
}

TIMEVAL getElapsedTime(void)
{
	u16 timer = TIM_GetCounter(TIM4);
	/* Calculate the time difference */
	 return timer > last_time_set ? timer - last_time_set : last_time_set - timer; 
	/* I think this should be fixed to 16 bit substraction */
	//timer = timer - last_time_set;		
	//return timer;
}

/* TIM4 configure */
void TIM4_Configuration(void)
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
	TIM_OCInitTypeDef TIM_OCInitStructure;	
	NVIC_InitTypeDef NVIC_InitStructure;
	//GPIO_InitTypeDef GPIO_InitStructure;
	
	//inable rcc of timer
  	RCC_APB1PeriphClockCmd ( RCC_APB1Periph_TIM4, ENABLE );

    NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x2;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

	// Configure tim pin	
	TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
    TIM_TimeBaseStructure.TIM_Prescaler = 84 - 1;       
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;   
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; 
    TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;	
    TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);

	// Output Compare Timing Mode configuration: Channel1
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
	TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

	TIM_OC1Init(TIM4, &TIM_OCInitStructure);
	TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Disable);

	// TIM IT enable
	TIM_ITConfig(TIM4, TIM_IT_CC1, ENABLE);

	// TIM enable counter
	TIM_Cmd(TIM4, ENABLE);

}

void TIM4_IRQHandler(void)
{
	//u16 capture = 0;
	if (TIM_GetITStatus(TIM4, TIM_IT_CC1) != RESET)
	{
		TIM_ClearITPendingBit(TIM4, TIM_IT_CC1);
		last_time_set = TIM_GetCounter(TIM4);
		TimeDispatch();
	}
}

